package handler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import bean.Page;
import bean.Test;
import service.TestService;

@Controller
public class QueryEntities {
    @Autowired
    @Qualifier(value = "service")
    private TestService service;
    
    @RequestMapping(path = "/view", method = RequestMethod.GET)
    public String view() {
        return "index";
    }

    @RequestMapping(value = "/query")
    @ResponseBody
    public Page handleRequest(
            // （自动装配）siz的值等价于req.getParameter("siz")
            String siz, String ind,//（同理）ind的值等价于req.getParameter("ind")
            // @RequestParam注解用于将请求的指定参数与指定处理器方法中的指定参数绑定
            @RequestParam(name = "kw", required = false) String keyword,//字符串keyword的值等价于请求中的kw参数的值
            HttpServletRequest req, HttpServletResponse resp) {
        // 设置页面的容量和索引
        Page page;
        int var1 = siz == null || siz.equals("") ? 10 : Integer.parseInt(siz);
        int var2 = ind == null || ind.equals("") ? 1 : Integer.parseInt(ind);
        if (var1 == 10 && var2 == 1) {
            page = new Page();
            // 如果siz的值等于10但ind的值不等于1
        } else if (var1 == 10) {
            page = new Page(var2);
        } else {
            page = new Page(var1, var2);
        }
        // 设置页面的查询条件
        Test test = new Test(keyword);
        page.setCondition(test);
        page = service.query(page);
        return page;
    }
}
